package com.battery.web.controller.system.battery;

import java.math.BigDecimal;
import java.util.List;

import com.battery.common.utils.DateUtils;
import com.battery.common.utils.ali.AliPayEntity;
import com.battery.framework.config.properties.AliPayResource;
import com.battery.system.domain.BO.EndorderBO;
import com.battery.system.domain.VO.EndorderVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.battery.common.annotation.Log;
import com.battery.common.enums.BusinessType;
import com.battery.system.domain.Endorder;
import com.battery.system.service.IEndorderService;
import com.battery.common.core.controller.BaseController;
import com.battery.common.core.domain.AjaxResult;
import com.battery.common.utils.poi.ExcelUtil;
import com.battery.common.core.page.TableDataInfo;

/**
 * 订单强制结束申请Controller
 *
 * @author battery
 * @date 2021-03-23
 */
@Controller
@RequestMapping("/system/endOrder")
public class EndorderController extends BaseController {


    private String prefix = "system/endOrder";

    @Autowired
    private IEndorderService endorderService;

    @Autowired
    private AliPayResource aliPayResource;

    private AliPayEntity _callApi() {
        AliPayEntity entity = new AliPayEntity();
        entity.setAppId(aliPayResource.getAppId());
        entity.setAlipayPublicKey(aliPayResource.getAlipayPublicKey());
        entity.setCharset(aliPayResource.getCharset());
        entity.setGatewayUrl(aliPayResource.getGatewayUrl());
        entity.setMerchantPrivateKey(aliPayResource.getMerchantPrivateKey());
        entity.setSignType(aliPayResource.getSignType());
        entity.setNotifyUrl(aliPayResource.getNotifyUrl());
        entity.setPayeeLogonId(aliPayResource.getPayeeLogonId());
        entity.setPayeeUserId(aliPayResource.getPayeeUserId());
        entity.setFreezeNotifyUrl(aliPayResource.getFreezeNotifyUrl());
        entity.setServiceId(aliPayResource.getServiceId());
        return entity;
    }


    @RequiresPermissions("system:endOrder:view")
    @GetMapping()
    public String endOrder() {
        return prefix + "/endOrder";
    }

    /**
     * 查询订单强制结束申请列表
     */
    @RequiresPermissions("system:endOrder:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(EndorderBO endorderBO) {
        startPage();
//        List<Endorder> list = endorderService.selectEndorderList(endorder);
        List<EndorderVO> list = endorderService.selectEndorderVOList(endorderBO);
        return getDataTable(list);
    }

    /**
     * 导出订单强制结束申请列表
     */
    @RequiresPermissions("system:endOrder:export")
    @Log(title = "订单强制结束申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Endorder endorder) {
        List<Endorder> list = endorderService.selectEndorderList(endorder);
        ExcelUtil<Endorder> util = new ExcelUtil<Endorder>(Endorder.class);
        return util.exportExcel(list, "endOrder");
    }

    /**
     * 新增订单强制结束申请
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存订单强制结束申请
     */
    @RequiresPermissions("system:endOrder:add")
    @Log(title = "订单强制结束申请", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Endorder endorder) {
        return toAjax(endorderService.insertEndorder(endorder));
    }

    /**
     * 修改订单强制结束申请
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        Endorder endorder = endorderService.selectEndorderById(id);
        mmap.put("endorder", endorder);
        return prefix + "/edit";
    }

    /**
     * 修改保存订单强制结束申请
     */
    @RequiresPermissions("system:endOrder:edit")
    @Log(title = "订单强制结束申请", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Endorder endorder) {
        return toAjax(endorderService.updateEndorder(endorder));
    }

    /**
     * 删除订单强制结束申请
     */
    @RequiresPermissions("system:endOrder:remove")
    @Log(title = "订单强制结束申请", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(endorderService.deleteEndorderByIds(ids));
    }


    /**
     * 跳转到拒绝申请操作页面
     *
     * @param id
     * @param mmap
     * @return
     */
    @GetMapping("/refuseCz/{id}")
    public String refuseCz(@PathVariable("id") String id, ModelMap mmap) {
        mmap.put("id", id);
        return prefix + "/refuseCz";
    }


    @Log(title = "强制结束申请拒绝操作", businessType = BusinessType.OTHER)
    @RequiresPermissions("system:endOrder:refuseCz")
    @PostMapping("/refuseCz")
    @ResponseBody
    public AjaxResult refuseCz(Endorder endorder) {
        if (endorder.getId() == null) {
            return error("主键ID为空");
        }

        if (StringUtils.isBlank(endorder.getRemark())) {
            return error("请填写拒绝理由");
        }

        // 审批状态 1 待审批 2 通过 3 拒绝
        endorder.setAuditStatus(3);
        endorder.setAuditTime(DateUtils.getNowDate());

        return toAjax(endorderService.updateByPrimaryKeySelective(endorder));
    }


    /**
     * 跳转到结束订单界面
     *
     * @param id
     * @return
     */
    @GetMapping("/endUsed/{id}")
    public String endUsed(@PathVariable("id") String id, ModelMap mmap) {
        mmap.put("id", id);
        return prefix + "/endUsed";
    }

    @RequiresPermissions("system:endOrder:endUsed")
    @Log(title = "强制结束租借订单", businessType = BusinessType.OTHER)
    @PostMapping("/endUsed")
    @ResponseBody
    public AjaxResult endUsed(EndorderBO endorderBO) {

        if (StringUtils.isBlank(endorderBO.getId())) {
            return error("主键ID为空");
        }

        if (endorderBO.getAmountPaid() == null) {
            return error("请输入扣费金额");
        }


        if (endorderBO.getAmountPaid().compareTo(BigDecimal.ZERO) < 0) {
            return error("填写的金额不能小于0");
        }

        AliPayEntity entity = _callApi();
        return endorderService.forceEndUsed(endorderBO, entity);
    }


}
